<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<%@ include file="/WEB-INF/views/include/srceasyui.jsp"%>

</head>
<body>
	<div>
		<form id="mainform" action="" method="post">
			<table class="formTable">
				<tr>
					<td>快递企业：</td>
					<td>
						<input id="expressName" name="expressName" class="easyui-validatebox" data-options="width: 250" value="">
					</td>
				</tr>
				<tr>
					<td>运单号：</td>
					<td>
						<input id="deliverySn" name="deliverySn" class="easyui-validatebox" data-options="width: 250" value="">
					</td>
				</tr>
			</table>
		</form>
		<br/>
		<table id="dg1"></table>
	</div>

	<div id="tb1" style="padding:5px;height:auto">
		<div>
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="append();">添加</a>
			<span class="toolbar-item dialog-tool-separator"></span>
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="saveRow();">保存修改行</a>
			<span class="toolbar-item dialog-tool-separator"></span>
			<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" data-options="disabled:false"  onclick="removeit()">删除</a>
			<span class="toolbar-item dialog-tool-separator"></span>

		</div>

	</div>
<script type="text/javascript">
	var dg1;

	$(function(){
		dg1=$('#dg1').datagrid({
			method: "get",
			// url:'${ctx}/business/seller/json?filter_EQR_sellerStatus=0',
			// url:'${ctx}/business/order/deliveryorderlistjson/${oi.orderId}',
			// fit : true,
			height: 300,
			fitColumns : true,
			border : true,
			striped:true,
			idField : 'id',
			pagination:false,
			rownumbers:true,
			singleSelect:true,
			columns:[[
				{field:'recId',title:'id', hidden: true},
				{field:'goodsId',title:'商品名称',width:100,
					formatter:function(value,row){
						return row.goodsName;
					},
					editor:{
						type:'combobox',
						options:{
							valueField:'goodsId',
							textField:'goodsName',
							method:'get',
							// url:'products.json',
							url: '${ctx}/business/order/allgoodsjson/${orderId}',
							onSelect:function(record){
								// alert(record.goodsPrice);
								var goodsPrice = $('#dg1').datagrid('getEditor', {'index':editIndex,'field':'goodsPrice'}).target;
								var goodsNumber = $('#dg1').datagrid('getEditor', {'index':editIndex,'field':'goodsNumber'}).target;
								var speNote = $('#dg1').datagrid('getEditor', {'index':editIndex,'field':'speNote'}).target;

								$(goodsPrice).val(record.goodsPrice);
								$(goodsNumber).val(record.goodsNumber);
								$(speNote).val(record.speNote);
								goodsPrice.prop('readonly', true);
								speNote.prop('readonly', true);
							}
						}
					}},
				{field:'goodsPrice',title:'订单中价格', width:100, editor:'text'},
				{field:'goodsNumber',title:'订单中数量',width:100, editor: {type:'text',
					options:{
						required:true
					}}},
				{field:'speNote',title:'特产备注',width:100, editor:'text'}
			]],
			enableHeaderClickMenu: false,
			enableHeaderContextMenu: false,
			enableRowContextMenu: false,
			toolbar:'#tb1'
		});
	});

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#dg1').datagrid('validateRow', editIndex)){
			var ed = $('#dg1').datagrid('getEditor', {index:editIndex,field:'goodsId'});
			var goodsName = $(ed.target).combobox('getText');
			$('#dg1').datagrid('getRows')[editIndex]['goodsName'] = goodsName;
			$('#dg1').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}

/*
	function onClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#dg1').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#dg1').datagrid('selectRow', editIndex);
			}
		}
	}
*/

	function append(){
		if (endEditing()){
//			$('#dg1').datagrid('appendRow',{});
//			editIndex = $('#dg1').datagrid('getRows').length-1;
//			$('#dg1').datagrid('selectRow', editIndex)
//					.datagrid('beginEdit', editIndex);
			dg1.datagrid('insertRow', {
				index: 0,	// 索引从0开始
				row: {}
			});
			editIndex = 0;
			dg1.datagrid('selectRow', editIndex)
					.datagrid('beginEdit', editIndex);
		}
	}
	function removeit(){
		if (editIndex == undefined) {
			var row = dg1.datagrid('getSelected');
			var index = dg1.datagrid('getRowIndex', row);
			dg1.datagrid('deleteRow', index);
			return;
		} else {
			dg1.datagrid('cancelEdit', editIndex)
					.datagrid('deleteRow', editIndex);
			editIndex = undefined;
		}
	}
	function saveRow(){
		endEditing();
	}

	function check() {
		// alert('check');
		if (editIndex != undefined) {

			alert('有未保存的记录!');
			return false;
		}

		return true;
	}
</script>
</body>
</html>